package com.gk.javaservice.controller;

import com.alibaba.fastjson.JSONArray;
import com.gk.javaservice.common.ServerResponse;
import com.gk.javaservice.po.AdminPermissions;
import com.gk.javaservice.service.AdminPermissionsService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/AdminPermissions")
public class AdminPermissionsController {

    @Autowired
    private AdminPermissionsService baseService;
    private Logger log = Logger.getLogger(this.getClass());


    //添加
    @CrossOrigin
    @ResponseBody
    @RequestMapping("/add")
    public ServerResponse<?> add(@RequestBody AdminPermissions t) {
        log.info(t);
        try {
            baseService.add(t);
            return ServerResponse.createBySuccess(t.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMessage("值重复");
        }


    }

    //根据ID删除1个
    @CrossOrigin
    @ResponseBody
    @RequestMapping("/delete")
    public ServerResponse<?> delete(@RequestBody AdminPermissions t) {
        log.info("id:" + t.getId());
        baseService.delete(t.getId());
        return ServerResponse.createBySuccess(t.getId());

    }

    //修改信息
    @CrossOrigin
    @ResponseBody
    @RequestMapping("/update")
    public ServerResponse<?> update(@RequestBody AdminPermissions t) {
        log.info("id:" + t.getId());

        try {
            baseService.update(t);
            return ServerResponse.createBySuccess(baseService.hasById(t.getId()));
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMessage("值重复");
        }
    }

    //获取所有信息
    @CrossOrigin
    @ResponseBody
    @RequestMapping("/hasAll")
    public ServerResponse<?> hasAll() {
        log.info("in to hasAll");
        return ServerResponse.createBySuccess(baseService.hasAll());

    }

    @CrossOrigin
    @ResponseBody
    @RequestMapping("/hasAllForJSON")
    public ServerResponse<?> hasAllForJSON() {

        log.info("in to hasAllForJSON");
        List<AdminPermissions> alist = new ArrayList<AdminPermissions>();
        alist = baseService.hasAll();
        JSONArray res = new JSONArray();
        for (AdminPermissions aa : alist) {
            if (aa.getParentId() == 0) {
                aa = setC(aa, alist);
                res.add(aa);
            }

        }
        return ServerResponse.createBySuccess(res);

    }

    public AdminPermissions setC(AdminPermissions aa, List<AdminPermissions> alist) {
        aa.setChildren(new ArrayList<>());
        for (AdminPermissions bb : alist) {
            if (bb.getParentId() == aa.getId()) {
                bb = setC(bb, alist);
                aa.getChildren().add(bb);
            }

        }

        return aa;
    }

}
